<?php
namespace Cms\Controller;
use Cms\Controller\BaseController;
class GroupController extends BaseController {
	
	public function _initialize() {
		//你可以在此覆盖父类方法	
		parent::_initialize();
	}	

/*权限组管理部分开始*/	

	public function groupList(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'系统设置',
				'url'=>U('Cms/User/#')
			),
			'1'=>array(
				'name'=>'权限组列表',
				'url'=>U('Cms/Group/groupList')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//绑定搜索条件与分页
		$m=M('group',"t_");
		$p=$_GET['p']?$_GET['p']:1;
	/*搜索功能*/
		$name=I('name')?I('name'):'';
		if($name){
			$map['name']=array('like',"%$name%");
			$map['duty_no']=array('like',"%$name%");
			$map['_logic'] = 'OR';
			$this->assign('name',$name);
		}

		$psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
		$cache=$m->where($map)->page($p,$psize)->select();
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'Ystech-loader', '权限组列表','Ystech-search');
		$this->assign('cache',$cache);		
		$this->display();
	}
	
	public function groupSet(){
		$id=I('id');
		$m=M('group',"t_");
		//设置面包导航，主加载器请配置		
		$bread=array(
			'0'=>array(
				'name'=>'系统设置',
				'url'=>U('Cms/User/#')
			),
			'1'=>array(
				'name'=>'权限组列表',
				'url'=>U('Cms/Group/groupList')
			),
			'1'=>array(
				'name'=>'权限组编辑',
				'url'=>U('Cms/Group/groupSet',array('id'=>$id))
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//处理POST提交	
		if(IS_POST){
			$data=I('post.');
			$data['user_id']=$_SESSION['CMS']['uid'];
            $data['user_name']=$_SESSION['CMS']['user']['username'];

			 //var_dump($data);
			/*部门编号*/
			if($data['duty_no']) {
				$wh = array('duty_no'=>$data['duty_no']);
				if($id) $wh['id'] = array('neq',$id);
				if($m->where($wh)->count()){
					$info['status']=0;
					$info['msg']='职位编号重复！';
					$this->ajaxReturn($info);
				}
			}			
			
			if($id){
				$data['update_time']=time();
				$re=$m->save($data);
				if(FALSE!==$re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}else{
				unset($data['id']);
				$data['create_time']=time();
				$re=$m->add($data);//echo D()->_sql();
				if($re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}
			$this->ajaxReturn($info);
		}
		
		//处理编辑界面
		if($id){			
			$cache=$m->where('id='.$id)->find();
			$this->assign('cache',$cache);	
		}
		
		$this->display();
	}

/*启用 ====> 禁用*/	
	public function groupDel(){
		$id=$_GET['id'];//必须使用get方法
		//dump($id);
		$m=M('group',"t_");
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$ids = explode(",",$id);
		//dump($ids);
		$array['is_del'] = 1;
		$array['update_time'] = time();
		$re=$m->where(array('id'=>array('in',$ids)))->save($array);
		if($re){
			$info['status']=1;
			$info['msg']='禁用成功!';
		}else{
			$info['status']=0;
			$info['msg']='禁用失败!';
		}
		$this->ajaxReturn($info);
	}

/*禁用 ====> 启用*/	
	public function groupReback(){
		$id=$_GET['id'];//必须使用get方法
		$m=M('group',"t_");
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$ids = explode(",",$id);
		$array['is_del'] = 0;
		$array['update_time'] = time();
		$re=$m->where(array('id'=>array('in',$ids)))->save($array);
		if($re){
			$info['status']=1;
			$info['msg']='启用成功!';
		}else{
			$info['status']=0;
			$info['msg']='启用失败!';
		}
		$this->ajaxReturn($info);
	}

/*权限组管理部分结束*/	
	
}